/*
 *  Blazon: A user datagram protocol re-braodcasting daemon.
 *  hash.h: Header file for hash.c
 *
 *  Copyright 2012 Blazon development team
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 of the License, or
 *  (at your option) any later version.

 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.

 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef INCLUDED_hash_h
#define INCLUDED_hash_h

#define FNV1_32_INIT	0x811c9dc5UL
#define MAX_LOAD_FACTOR	0.65f
#define KEYLEN	25


struct hashpkt {
	time_t timestamp;
};


typedef struct _hash_node hash_node;

struct _hash_node
{
	char key[KEYLEN+1];
	void *data;
	hash_node *next;
};

struct hashtable
{
	hash_node **table;
	unsigned short bitsize;
	unsigned int count;
	unsigned int loadlimit;
	unsigned short keylen;
};

extern struct hashtable * hashtable_create(unsigned short);
extern uint32_t hash_index(struct hashtable *, const unsigned char *s);
extern bool hashnode_add(struct hashtable *, void *, void *);
extern void * hashnode_find_delete(struct hashtable *, void *);
extern void * hashnode_find(struct hashtable *, void *);
extern bool hashnode_update(struct hashtable *, void *, void *);
extern void hashtable_destroy(struct hashtable *, short);
extern void * hashnode_delete(hash_node *, hash_node **);


#define HNODE_FOREACH(pos, head) for (pos = (head); pos != NULL; pos = pos->next)
#define HNODE_FOREACH_SAFE(pos, n, head) for (pos = (head), n = pos ? pos->next : NULL; pos != NULL; pos = n, n = pos ? pos->next : NULL)


#endif
